package com.mc.fil.service.impl;

import java.util.List;

import com.mc.common.core.domain.model.LoginUser;
import com.mc.common.utils.DateUtils;
import com.mc.common.utils.ServletUtils;
import com.mc.fil.domain.FilMultimedia;
import com.mc.fil.domain.FilWatering;
import com.mc.fil.mapper.FilMultimediaMapper;
import com.mc.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mc.fil.mapper.FilIntoMachingMapper;
import com.mc.fil.domain.FilIntoMaching;
import com.mc.fil.service.IFilIntoMachingService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 转入加工中心 Service业务层处理
 *
 * @author mc
 * @date 2021-04-14
 */
@Service
public class FilIntoMachingServiceImpl implements IFilIntoMachingService
{
    @Autowired
    private FilIntoMachingMapper filIntoMachingMapper;
    @Autowired
    private FilMultimediaMapper multimediaMapper;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询转入加工中心
     *
     * @param id 转入加工中心 ID
     * @return 转入加工中心
     */
    @Override
    public FilIntoMaching selectFilIntoMachingById(Long id)
    {
        FilIntoMaching maching = filIntoMachingMapper.selectFilIntoMachingById(id);
        FilMultimedia multimedia = new FilMultimedia();
        multimedia.setFilId(id);
        multimedia.setGenre("filIntoMaching");
        maching.setPhotos(multimediaMapper.selectFilMultimediaList(multimedia));
        return maching;
    }

    /**
     * 查询转入加工中心 列表
     *
     * @param filIntoMaching 转入加工中心
     * @return 转入加工中心
     */
    @Override
    public List<FilIntoMaching> selectFilIntoMachingList(FilIntoMaching filIntoMaching)
    {
        List<FilIntoMaching> list = filIntoMachingMapper.selectFilIntoMachingList(filIntoMaching);
        for(FilIntoMaching maching : list){
            FilMultimedia multimedia = new FilMultimedia();
            multimedia.setFilId(maching.getId());
            multimedia.setGenre("filIntoMaching");
            maching.setPhotos(multimediaMapper.selectFilMultimediaList(multimedia));
        }
        return list;
    }

    /**
     * 新增转入加工中心
     *
     * @param filIntoMaching 转入加工中心
     * @return 结果
     */
    @Transactional
    @Override
    public int insertFilIntoMaching(FilIntoMaching filIntoMaching)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        filIntoMaching.setCreateTime(DateUtils.getNowDate());
        filIntoMaching.setCreateUserId(loginUser.getUsername());
        filIntoMachingMapper.insertFilIntoMaching(filIntoMaching);
        List<FilMultimedia> list = filIntoMaching.getPhotos();
        for(FilMultimedia fil : list){
            fil.setFilId(filIntoMaching.getId());
            fil.setCreatedBy(loginUser.getUsername());
            fil.setCreatedTime(DateUtils.getNowDate());
            fil.setGenre("filIntoMaching");
            multimediaMapper.insertFilMultimedia(fil);
        }
        return 1;
    }

    @Override
    public int updateFilIntoMaching(FilIntoMaching filIntoMaching) {
        return filIntoMachingMapper.updateFilIntoMaching(filIntoMaching);
    }

}